package com.pets.controller;

import com.pets.entity.Users;
import com.pets.enums.YesNoEnum;
import com.pets.qo.ResetPasswordQO;
import com.pets.qo.UserLoginQO;
import com.pets.qo.UserRegisterQO;
import com.pets.service.PhonecodeService;
import com.pets.service.UserService;
import com.pets.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户相关功能控制器
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PhonecodeService phoneCodeService;

    /**
     * 到用户端首页
     *
     * @return
     */
    @RequestMapping(value = "/toIndex", method = RequestMethod.GET)
    public String toIndex() {
        return "user/index";
    }


    /**
     * 用户登录
     *
     * @param request
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    public Result userLogin(HttpServletRequest request, UserLoginQO userLoginQO) {

        return userService.userLogin(request, userLoginQO);
    }

    /**
     * 校验手机号是否被注册
     *
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkPhone", method = RequestMethod.POST)
    public Result checkPhone(String phone) {
        Result result = userService.checkPhone(phone);
        return result;
    }

    /**
     * 校验用户名是否被注册
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkUserName", method = RequestMethod.POST)
    public Result checkUserName(String uName) {
        Result result = userService.checkUserName(uName);
        return result;
    }

    /**
     * 发送手机验证码
     *
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendPhoneCode", method = RequestMethod.POST)
    public Result sendPhoneCode(String phone) {
        phoneCodeService.sendPhoneCode(phone);
        Result result = new Result();
        result.setCode(YesNoEnum.YES.getCode());
        result.setMsg("发送成功");
        return result;
    }

    /**
     * 用户注册
     *
     * @param userRegisterQO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userRegister", method = RequestMethod.POST)
    public Result userRegister(UserRegisterQO userRegisterQO) {
        Result result = userService.userRegister(userRegisterQO);
        return result;
    }

    /**
     * 到用户登录界面
     *
     * @return
     */
    @RequestMapping(value = "/toLogin", method = RequestMethod.GET)
    public String toLogin() {
        return "user/login";
    }

    /**
     * 到用户注册界面
     *
     * @return
     */
    @RequestMapping(value = "/toRegister", method = RequestMethod.GET)
    public String toRegister() {
        return "user/register";
    }

    /**
     * 到重置密码界面
     *
     * @return
     */
    @RequestMapping(value = "/toResetPassword", method = RequestMethod.GET)
    public String toResetPassword() {
        return "user/resetPassword";
    }

    /**
     * 重置密码查看是否存在该手机账户
     *
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resetPasswordGetUserByPhone", method = RequestMethod.POST)
    public Result resetPasswordGetUserByPhone(String phone) {
        Result result = userService.resetPasswordGetUserByPhone(phone);
        return result;
    }

    /**
     * 重置密码
     *
     * @param resetPasswordQO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Result resetPassword(ResetPasswordQO resetPasswordQO) {
        Result result = userService.resetPassword(resetPasswordQO);
        return result;
    }

    /**
     * 跳转到个人中心
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/toPersonal")
    public ModelAndView toPersonal(int userId) {
        ModelAndView modelAndView = new ModelAndView();
        Users users = userService.getUsersById(userId);
        modelAndView.addObject("users", users);
        modelAndView.setViewName("/user/personal");//转发
        return modelAndView;
    }

    /**
     * 修改用户密码
     *
     * @param userId
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userUpdatePassword")
    public Result userUpdatePassword(int userId, String password) {
        Result result = userService.userUpdatePassword(userId, password);
        return result;
    }

    /**
     * 用户退出操作
     *
     * @return
     */
    @RequestMapping(value = "/userExit")
    public String userExit(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "user/index";
    }

    /**
     * 用户实名认证
     * @param userId
     * @param cardNo
     * @param realName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/realNameCertification" , method = RequestMethod.POST)
    public Result realNameCertification(int userId,String cardNo, String realName){
        Result result = userService.realNameCertification(userId,cardNo,realName);
        return result;
    }
}
